package com.woniuxy.carlife.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.domain.UserServiceLike;
import com.woniuxy.carlife.mapper.UserServiceLikeMapper;
import com.woniuxy.carlife.param.UserLikeServiceParam;
import com.woniuxy.carlife.service.UserServiceLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author fx
 * @ClassName:CouponCategoryController
 * @Description: 用户关联的服务点赞service实现类
 * @date 2021/4/14 19:36
 **/
@Service
public class UserServiceLikeServiceImpl extends ServiceImpl<UserServiceLikeMapper, UserServiceLike> implements UserServiceLikeService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserServiceLikeMapper mapper;

    /**
     * 功能描述〈新增用户点赞〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/14 19:45
     * @throws
     */
    @Override
    public boolean saveLikeByUserIdAndServiceId(UserLikeServiceParam userLikeServiceParam) {
        //调用根据用户id和服务id的查询方法
        UserServiceLike like = new UserServiceLike();
        //服务id
        like.setServiceId(userLikeServiceParam.getServiceId());
        //用户ID
        like.setUserId(userLikeServiceParam.getUserId());
        //点赞状态，默认为点赞
        like.setServiceLikeStatus("点赞");
        //点赞时间
        like.setServiceLikeCreateTime(new Timestamp(new Date().getTime()));
        //先存数据库
        int insert = mapper.insert(like);
        if (insert>0){
            //先存redis
            //把当前数据转换成json字符串
            String str = JSON.toJSONString(like);
            //存redis,string类型，把用户ID和服务ID当成key存
            redisTemplate.opsForValue()
                    .set("com.woniuxy:carlife:serviceproject:userlikeservice:userid:"
                                    +like.getUserId()+"serviceid:"+like.getServiceId(),
                            str);
            return true;
        }
        return false;
    }

    /**
     * 功能描述〈根据用户id和服务id修改点赞状态为取消点赞或点赞,在reids改，定时改到数据库〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/14 19:47
     * @throws
     */
    @Override
    public String updateLikeToOffByUserIdAndServiceId(UserLikeServiceParam like) {
        UserServiceLike like1 = findLikeStatusByUserIdAndServiceId(like);
        //创建一个字符串返回
        String str;
        if (ObjectUtils.isEmpty(like1)){
            //如果没查到说明是第一次点赞，调用
            boolean b = saveLikeByUserIdAndServiceId(like);
            if (!b){
               //新增失败才返回错误
               return "错误";
            }else{
                //必定能查到
                String s =(String) redisTemplate.opsForValue()
                        .get("com.woniuxy:carlife:serviceproject:userlikeservice:userid:"
                                + like.getUserId() + "serviceid:" + like.getServiceId());
                like1=JSON.parseObject(s,UserServiceLike.class);
                //新用户点赞后就把新用户的key删除
                redisTemplate.delete
                        ("com.woniuxy:carlife:serviceproject:userlikeservice:userid:"
                                +like.getUserId()+"serviceid:"+like.getServiceId());
                str="点赞";
            }
        }else{
            //不是第一次新增才根据状态修改
            //判断当前是什么状态，根据状态改变
            if (like1.getServiceLikeStatus().equals("点赞")){
                //改状态为取消点赞
                like1.setServiceLikeStatus("取消点赞");
                str="取消点赞";
            }else{
                ///改状态为点赞
                like1.setServiceLikeStatus("点赞");
                str="点赞";
            }
        }
        //修改时间
        like1.setServiceLikeUpdateTime(new Timestamp(new Date().getTime()));
        //先存redis
        //把当前数据转换成json字符串
        String str1 = JSON.toJSONString(like1);
        //存redis,string类型，把用户ID和服务ID当成key存
        redisTemplate.opsForValue()
                .set("com.woniuxy:carlife:serviceproject:userlikeservice:userid:"
                                +like.getUserId()+"serviceid:"+like.getServiceId(),
                        str1);
        //设个过期时间,5分钟
        redisTemplate.expire("com.woniuxy:carlife:serviceproject:userlikeservice:userid:"
                        +like.getUserId()+"serviceid:"+like.getServiceId(),
                5*60,TimeUnit.SECONDS);
        return str;
    }

    /**
     * 功能描述〈根据用户id和服务id查看点赞状态〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/14 19:47
     * @throws
     */
    @Override
    public UserServiceLike findLikeStatusByUserIdAndServiceId(UserLikeServiceParam like) {
        String s =(String) redisTemplate.opsForValue()
                .get("com.woniuxy:carlife:serviceproject:userlikeservice:userid:"
                        + like.getUserId() + "serviceid:" + like.getServiceId());
        UserServiceLike like1;
        if (ObjectUtils.isEmpty(s)){
            QueryWrapper<UserServiceLike> qw = new QueryWrapper<>();
            qw.eq("user_id",like.getUserId());
            qw.eq("service_id",like.getServiceId());
            like1 = mapper.selectOne(qw);
        }else{
            like1 = JSON.parseObject(s, UserServiceLike.class);
        }
        return like1;
    }

    /**
     * 功能描述〈修改是点赞的状态到数据库〉
     * @author:fx
     * @param: @param null:
     * @Date 2021/4/14 21:35
     * @throws
     */
    @Override
    public void updateLikeStatusByUserIdAndServiceIdToSql(List<UserLikeServiceParam> like) {
        //创建个关联表对象放状态
        UserServiceLike like1 = new UserServiceLike();
        //循环并修改状态为点赞到数据库
        for (UserLikeServiceParam u : like) {
            QueryWrapper<UserServiceLike> qw = new QueryWrapper<>();
            qw.eq("user_id",u.getUserId());
            qw.eq("service_id",u.getServiceId());
            //用户id
            like1.setUserId(u.getUserId());
            //服务id
            like1.setServiceId(u.getServiceId());
            //状态设置为点赞
            like1.setServiceLikeStatus("点赞");
            //循环修改到数据库
            int i = mapper.update(like1, qw);
        }
    }

    /**
     * 功能描述〈修改取消点赞状态到数据库〉
     * @author:fx
     * @param: @param null:
     * @Date 2021/4/14 21:35
     * @throws
     */
    @Override
    public void updateOffLikeStatusByUserIdAndServiceIdToSql(List<UserLikeServiceParam> like) {
        //创建个关联表对象放状态
        UserServiceLike like1 = new UserServiceLike();
        //循环并修改状态为点赞到数据库
        for (UserLikeServiceParam u : like) {
            QueryWrapper<UserServiceLike> qw = new QueryWrapper<>();
            qw.eq("user_id",u.getUserId());
            qw.eq("service_id",u.getServiceId());
            //用户id
            like1.setUserId(u.getUserId());
            //服务id
            like1.setServiceId(u.getServiceId());
            //状态设置为点赞
            like1.setServiceLikeStatus("取消点赞");
            //循环修改到数据库
            int i = mapper.update(like1, qw);
        }
    }
}
